package com.itsm.common.util;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;

public class JwtUtil {

    private static String secert = "ABC123!@#";
    //过期时间1天
    public static final int EXPIRATION = 3600;
    //过期时间7天
    public static final int EXPIRATION_REMEMBER = 604800;

    public static String getToken(String userName, Boolean isRememberMe) {

        int expiration = isRememberMe ? EXPIRATION_REMEMBER : EXPIRATION;
        
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND,expiration);

        String token = JWT.create().withClaim("username", userName).withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(secert));

        return token;
    }

    public static DecodedJWT verify(String token){
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(secert)).build().verify(token);

        return decodedJWT;
    }

    public static DecodedJWT getTokenInfo(String token){
        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(secert)).build().verify(token);
        return decodedJWT;
    }

}
